//
//  LoginView.m
//  StationManager
//
//  Created by 泽华伟业 on 17/3/16.
//  Copyright © 2017年 Helong. All rights reserved.
//

#import "LoginView.h"
#import "DataHandel.h"
#import "UserInfoModel.h"
#import "MapViewController.h"
#import "HLNavigationController.h"

@interface LoginView ()

@property (nonatomic,strong) UITextField *userNameTF;

@property (nonatomic,strong) UITextField *passwordTF;

@property (nonatomic,strong) UIButton *loginButton;

@end

@implementation LoginView

- (instancetype)init
{
    self = [super init];
    if (self) {
       self.userNameTF.placeholder = @"用户名";
       self.passwordTF.placeholder = @"密码";
        [self loginButton];
    }
    return self;
}

- (UITextField *)userNameTF{
    if (!_userNameTF) {
        _userNameTF = [[UITextField alloc]init];
        [self addSubview:_userNameTF];
        _userNameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_userNameTF setFont:TEXT_FONT];
        _userNameTF.borderStyle = UITextBorderStyleRoundedRect;
        if ([USER_DEFAULT objectForKey:@"userName"]) {
            _userNameTF.text = [USER_DEFAULT objectForKey:@"userName"];
        }
        [_userNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(120*hl);
            make.left.equalTo(self).offset(10*wl);
            make.right.equalTo(self.mas_right).offset(-10*wl);
            make.height.mas_equalTo(50*hl);
        }];
    }
    return _userNameTF;
}

- (UITextField *)passwordTF{
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc]init];
        [self addSubview:_passwordTF];
        _passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        [_passwordTF setFont:TEXT_FONT];
        [_passwordTF setSecureTextEntry:YES];
        _passwordTF.borderStyle = UITextBorderStyleRoundedRect;
        [_passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_userNameTF.mas_bottom).offset(10*hl);
            make.size.left.equalTo(_userNameTF);
        }];
    }
    return _passwordTF;
}

- (UIButton *)loginButton{
    if (!_loginButton) {
        _loginButton = [[UIButton alloc]init];
        [self addSubview:_loginButton];
        [_loginButton setBackgroundColor:RGBCOLOR(54, 124, 207)];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        _loginButton.layer.masksToBounds = YES;
        _loginButton.layer.cornerRadius = 5;
        [_loginButton addTarget:self action:@selector(loginButtonDidSelected:) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_passwordTF.mas_bottom).offset(15*hl);
            make.left.right.equalTo(_passwordTF);
            make.height.mas_equalTo(45*hl);
        }];
    }
    return _loginButton;
}

//点击登录按钮
- (void)loginButtonDidSelected:(UIButton *)sender{
    if ([_passwordTF.text isEqualToString:@""] || [_userNameTF.text isEqualToString:@""]) {
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text = @"用户名或密码不能为空";
        hud.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5f];
        hud.offset = CGPointMake(0.f, 200);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        });
        return ;
    }
    [MBProgressHUD showHUDAddedTo:[self getController].view animated:YES];
    [[DataHandel sharedDataHandel]userLoginAndGetUserInfoByUserName:_userNameTF.text Password:_passwordTF.text Block:^(id object) {
        NSArray *array = (NSArray *)object;
        NSLog(@"%@",object);
        NSDictionary *userDict = array[0];
        if ([[userDict objectForKey:@"LoginStatue"] isEqualToString:@"登录成功"]) {
            UserInfoModel *userInfoModel = [[UserInfoModel alloc]init];
            [userInfoModel setValuesForKeysWithDictionary:userDict];
            //将用户信息存储起来
            NSData *userData = [NSKeyedArchiver archivedDataWithRootObject:userInfoModel];
            [USER_DEFAULT setObject:userData forKey:@"userInfoModel"];
            //用户名存储起来
            [USER_DEFAULT setObject:_userNameTF.text forKey:@"userName"];
            [USER_DEFAULT setObject:[userDict objectForKey:@"UserID"] forKey:@"userID"];
            [USER_DEFAULT setObject:[userDict objectForKey:@"Job"] forKey:@"Job"];
            [USER_DEFAULT setObject:[userDict objectForKey:@"UserLocation"] forKey:@"UserLocation"];
            [USER_DEFAULT setObject:[userDict objectForKey:@"czyPower"] forKey:@"czyPower"];

            //显示加载效果
            [MBProgressHUD hideHUDForView:[self getController].view animated:YES];
           // [[self getController] presentViewController:[[HLNavigationController alloc] initWithRootViewController:[[MapViewController alloc] init]] animated:YES completion:nil];
            [UIApplication sharedApplication].keyWindow.rootViewController = [[HLNavigationController alloc] initWithRootViewController:[[MapViewController alloc] init]];
        }else if ([[userDict objectForKey:@"LoginStatue"] isEqualToString:@"登录失败"]){
            [MBProgressHUD hideHUDForView:[self getController].view animated:YES];
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.label.text = @"登录失败,请检查用户名与密码是否匹配";
            hud.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5f];
            hud.offset = CGPointMake(0.f, 200);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
            });
        }
    }];
}
//获取视图控制器
- (UIViewController *)getController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vc =  (UIViewController*)nextResponder;
            return vc;
        }
    }
    return nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
